package com.tkmybatis.aop;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;

/**
 * Project_name: tk_mybatis_demo
 * Company: 新致软件
 * Author: ZHU YU WEI
 * Date: Created in 20:17 2021/4/4.
 * Description:
 * Modified By:
 */
@Component
@Aspect
@Slf4j
public class ControllerAspect {

    @Pointcut("execution(* com.tkmybatis.controller.*.*(..))")
    public void pointCut(){

    }


    @Around("pointCut()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable{
        System.out.println("环绕前通知");
        Object proceed = joinPoint.proceed();
        Object[] args = joinPoint.getArgs();
        System.out.println("args==="+JSON.toJSONString(args));
        //获取到想要执行方法
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        log.info("执行的方法是====="+method);
        String[] parameterNames = signature.getParameterNames();
        log.info("执行的方法参数是====="+JSON.toJSONString(parameterNames));
        log.info(JSON.toJSONString(proceed));
//        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
//        if(attributes!=null){
//            HttpServletRequest request = attributes.getRequest();
//            log.info("request==="+JSON.toJSONString(request));
//        }

        System.out.println("环绕后通知");
        return proceed;
    }


    @AfterThrowing("pointCut()")
    public void afterThrowing(){
        System.out.println("异常方法进行增强");
        log.error("异常方法进行增强");
    }
}
